﻿
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>passwordView</title>
    <link href="~/Content/bootstrap.min.css" rel="stylesheet" />
    <script src="~/scripts/jquery-1.10.2.min.js"></script>
    <script src="~/scripts/bootstrap.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#Modal-changePwd").modal("show");
        })
        //获取用户密码
        $(function () {
            $.ajax({
                url:"@Url.Action("getPwd", "Users")",
                type: "post",
            dataType: "json",
            success: function (data) {
                if (data != "") {
                    $("#txtOldpwd").val(data.oldPwd);
                    $("#txtisadmin").val(data.usercode);
                }
            }
            });
        })
        //修改用户密码
        $(function () {
            $("#btn-sure").on("click", function () {
                if ($("#txtisadmin").val() != "admin") {
                    $("#Modal-Info #message").text("亲，您不是管理员，不能随意修改密码哦(⊙o⊙)");
                    $("#Modal-Info").modal("show");
                } else {
                    //提交验证，防止用户强行提交空值或者非法值
                    var elements = $("#Modal-changePwd .form-group");
                    var classname = "";
                    for (i = 0; i < elements.length; i++) {
                        classname += elements[i].className;
                    }
                    //alert(classname);
                    if (classname.indexOf("has-error") != -1 || $("#confirmPwd").val() == "" || $("#newPwd").val() == "" || $("#oldPwd").val() == "") {
                        $("#Modal-Info #message").text("请检查输入密码是否符合要求");
                        $("#Modal-Info").modal("show");
                    } else {
                        $.ajax({
                            url: "@Url.Action("changePwd", "Users")",
                            type: "post",
                        data:{
                            "pwd": $("#confirmPwd").val()
                        },
                        dataType: "json",
                        success: function (data) {
                            if (data == "isok") {
                                $("#Modal-reLogin").modal("show");
                            }
                        }
                    });
                }
                }
            });
        })
        //修改成功，重新登录
        //function Logout() {
        //    $("#Modal-reLogin #LogoutForm").submit();
        //}
        //表单数据格式验证
        function Verify(name, txtlength, message) {
            var txtname = $("#" + name + "").val();
            switch (name) {
                case "oldPwd":
                    if (txtname.length > txtlength || txtname.length <= 0) {
                        NoPass(name, message);
                    } else {
                        if ($("#oldPwd").val() != $("#txtOldpwd").val()) {
                            NoPass(name, '密码输入错误');
                        }
                        else{
                            Pass(name);
                        }
                    }
                    break;
                case "newPwd":
                    if (txtname.length > txtlength || txtname.length <= 0) {
                        NoPass(name, message);
                    } else {
                        if ($("#newPwd").val() == $("#oldPwd").val()) {
                            NoPass(name, '新密码不得与旧密码相同');
                        }else{
                            Pass(name);
                        }
                    }
                    break;
                case "confirmPwd":
                    if (txtname.length > txtlength || txtname.length<=0) {
                        NoPass(name, message);
                    } else {
                        if (!($("#newPwd").val() == $("#confirmPwd").val())){
                            NoPass(name, '两次输入密码不一致');
                        }else{
                            Pass(name);
                        }
                    }
                    break;
            }
        }
        //验证通过回调函数
        function Pass(name) {
            $("#div_" + name + "").removeClass("has-error").addClass("has-success");
            $("#" + name + "Helper").text("OK!");
        }
        //验证失败回调函数
        function NoPass(name, msg) {
            $("#div_" + name + "").addClass("has-error").removeClass("has-success");
            $("#" + name + "Helper").text(msg);
        }
    </script>
    <style type="text/css">
        .mybtn:hover{
            background-color:#009688;
            color:white;
        }
    </style>
</head>
<body>
    <!-- Modal -->
    <div class="modal fade" id="Modal-changePwd" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content" style="width:400px;">
                <div class="modal-header" style="background-color:#009688;">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel" style="color:white;"><span class="glyphicon glyphicon-edit" aria-hidden="true"></span> 修改密码</h4>
                </div>
                <div class="modal-body text-center" style="font-family:Arial, Helvetica, sans-serif;color:#317400;font-size:15px;margin-left:40px;margin-right:40px;margin-top:20px;">
                    <div class="form-group" id="div_oldPwd">
                        <input type="password" class="form-control" id="oldPwd" onblur="Verify('oldPwd', 10, '密码不能为空且长度不得超过10个字符')" placeholder="请输入旧密码" />
                        <small id="oldPwdHelper" class="help-block">&nbsp;</small>
                    </div>
                    <div class="form-group" id="div_newPwd">
                        <input type="password" class="form-control" id="newPwd" onblur="Verify('newPwd', 10, '密码不能为空且长度不得超过10个字符')" placeholder="请输入新密码" />
                        <small id="newPwdHelper" class="help-block">&nbsp;</small>
                    </div>
                    <div class="form-group" id="div_confirmPwd">
                        <input type="password" class="form-control" id="confirmPwd" onblur="Verify('confirmPwd', 10, '密码不能为空且长度不得超过10个字符')" placeholder="请确认新密码" />
                        <small id="confirmPwdHelper" class="help-block">&nbsp;</small>
                    </div>
                    <input type="hidden" id="txtOldpwd" />
                    <input type="hidden" id="txtisadmin" />
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default mybtn" id="btn-sure" style="margin-right:38%;"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span> 确定修改</button>
                </div>
            </div>
        </div>
    </div>
    <!-- Modal -->
    <div class="modal fade" id="Modal-reLogin" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content" style="width:400px;">
                <div class="modal-header" style="background-color:#009688;">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel" style="color:white;"><span class="glyphicon glyphicon-info-sign"></span> 系统提示</h4>
                </div>
                <div class="modal-body text-center" style="font-family:Arial, Helvetica, sans-serif;color:#317400;font-size:15px;">
                    密码修改成功,请退出系统重新登录,感谢您的使用!
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default mybtn" style="margin-right:38%;"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span> 确定</button>
                </div>
            </div>
        </div>
    </div>
    <!-- Modal -->
    <div class="modal fade" id="Modal-Info" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content" style="width:400px;">
                <div class="modal-header" style="background-color:#009688;">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel" style="color:white;"><span class="glyphicon glyphicon-info-sign"></span> 系统提示</h4>
                </div>
                <div class="modal-body text-center" style="font-family:Arial, Helvetica, sans-serif;color:#317400;font-size:15px;">
                    <p id="message"></p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default mybtn" data-dismiss="modal" style="margin-right:38%;">关闭</button>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
